package com.yummy.controller;

import com.yummy.common.BaseResponse;
import com.yummy.common.TokenUtils;
import com.yummy.dto.AddressBookCopy;
import com.yummy.entity.AddressBook;
import com.yummy.entity.Category;
import com.yummy.entity.Employee;
import com.yummy.entity.User;
import com.yummy.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * @author yinbo
 */
@RestController
@RequestMapping("/address")
public class UserAddressController {
	@Autowired
	TokenUtils tokenUtils;

	@Autowired
	AddressService addressService;

	@GetMapping("/queryAddress")
	public BaseResponse queryByUser(HttpServletRequest request){
		User currentUser = tokenUtils.getCurrentUser(request);
		Integer userId = currentUser.getId();
		List<AddressBook> addressList = addressService.queryByUser(userId);
		return BaseResponse.success().data("addressList",addressList);
	}
	@GetMapping("/allAddressByUser")
	public BaseResponse allAddress(HttpServletRequest request){
		User currentUser = tokenUtils.getCurrentUser(request);
		Integer userId = currentUser.getId();
		List<AddressBookCopy> addressList = addressService.queryByUsers(userId);
		return BaseResponse.success().data("addressList",addressList);
	}
	@PostMapping("/add")
	public BaseResponse addAddress(@RequestBody AddressBook addressBook) {

		boolean result = addressService.addAddressBook(addressBook);
		if (result) {
			return BaseResponse.success().message("添加成功");
		}
		return BaseResponse.fail().message("添加失败");
	}

	@PostMapping("/update")
	public BaseResponse editAddress(@RequestBody AddressBook addressBook, HttpServletRequest request) throws NoSuchAlgorithmException {

		addressBook.setConsignee(addressBook.getConsignee());
		addressBook.setPhone(addressBook.getPhone());
		addressBook.setProvinceName(addressBook.getProvinceName());
		addressBook.setCityName(addressBook.getCityName());
		addressBook.setDistrictName(addressBook.getDistrictName());
		addressBook.setDetail(addressBook.getDetail());
		boolean result = addressService.editAddressBook(addressBook);
		if (result) {
			return BaseResponse.success().message("修改成功");
		}
		return BaseResponse.fail().message("修改失败");
	}

	@GetMapping("/remove")
	public BaseResponse removeAddress(@RequestParam String ids) {
		System.out.println("ids==>" + ids);
		boolean result = addressService.removeAddressBooks(ids);
		if (result) {
			return BaseResponse.success().message("删除成功");
		} else {
			return BaseResponse.fail().message("删除失败");
		}
	}

	@GetMapping("/setDefault/{id}/{isDefault}")
	public BaseResponse setDefault(@PathVariable("id") Integer id, @PathVariable("isDefault") Integer isDefault) {
		System.out.println(id + "----" + isDefault);
		boolean result = addressService.updateIsDefault(id, isDefault);
		if (result) {
			return BaseResponse.success().message("设置成功");
		}
		return BaseResponse.fail().data("isDefault", addressService.findAddressById(id).getIsDefault());
	}

}
